<template>
	<view>
		<my-search @click="gotoSearch" bgcolor='#c00000' :radius="15" placeholder="默认的placeholder的值" />
		<view class="scroll-view-container">
			<scroll-view scroll-y class="left-scroll-view" :style="{ height: wh + 'px'}">
				<block v-for="(item,index) in cateList" :key="index">
					<view :class="['left-srcoll-view-item', index === active ? 'active' : '']"
					@click="activeChanged(index)">{{ item.cat_name }}</view>
				</block>
			</scroll-view>
			<!-- 右侧 -->
			<scroll-view scroll-y :scroll-top="scrollTop" class="right-scroll-view" 
			             :style="{ height:wh + 'px'}" >
				<!-- 二级标题 -->
				<view class="cate-leve" v-for="(item, index) in cateLeve" :key="index">
					<view class="cate-leve-title">/{{ item.cat_name}}/</view>
					<!-- 三级列表 -->
					<view class="acte-leves-list">
						<view class="cate-leves-item" v-for="(items,indexs) in item.children" :key="indexs" @click="gotoGoodsList(items)">
							<!-- 图片 -->
							<image :src="items.cat_icon" mode=""></image>
							<!-- 文字 -->
							<text>{{items.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 窗口的可用高度=
				wh:0,
				cateList:[],//一级分类的数据列表
				cateLeve:[],//二级分类的数据列表
				active:0,//当前选中的激活项 默认第一个被选中
				scrollTop:0,
			};
		},
		onLoad(){
			// 获取当前系统的信息
			const sysInfo=uni.getSystemInfoSync()
			// 当前可使用窗口的高度
			this.wh=sysInfo.windowHeight - 50
			this.getCateList()
		},
		methods:{
			async getCateList(){
				// 请求数据
				const {data : res }=await uni.$http.get('/api/public/v1/categories')
				console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg()
				this.cateList=res.message
				// 给二级列表赋值
				this.cateLeve=res.message[0].children
			},
			activeChanged(index){
				this.active = index
				// 当点击的这个当前项发生变化的时候，为二级列表重新赋值
				this.cateLeve=this.cateList[index].children
				// 让scroll 的值再0和1之间切换
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			gotoGoodsList(item){
				console.log(item);
				uni.navigateTo({
					url:`/subpkg/goods_list/goods_list?cid=${item.cat_id}`
				})
			},
			gotoSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
.scroll-view-container{
	display: flex;
	.left-scroll-view{
		width: 120px;
		.left-srcoll-view-item{
			line-height: 60px;
			background-color:#F7F7F7;
			text-align: center;
			font-size: 12px;
			// 激活当前项的样式
			&.active {
				background-color: #ffffff;
				position: relative;
				// 当前激活项左侧的边框线
				&::before{
					content: ' ';
					display: block;
					width: 3px;
					height: 30px;
					background-color: #c00000;
					position: absolute;
					top: 50%;
					left: 0;
					transform: translateY(-50%);
				}
			}
		}
	}
	.cate-leve-title{
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px;
	}
	.acte-leves-list{
		display: flex;
		flex-wrap: wrap;
		.cate-leves-item{
			width: 33.33%;
			margin-bottom: 10px;
			display: flex;
			flex-direction: column;
			align-items: center;
			image{
				width: 60px;
				height: 60px;
			}
			text{
				font-size:12px ;
			}
		}
	}
}
</style>
